package com.bosch.in.rtp1.ept.action;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.bosch.in.rtp1.ept.action.form.SkippedRecipeConditionForm;
import com.bosch.in.rtp1.ept.manager.SkpFshRcpManager;
import com.bosch.in.rtp1.ept.model.dto.SkippedFshRcpCon;
import com.bosch.in.rtp1.ept.util.ConverterUtility;
import com.bosch.in.rtp1.ept.util.ManagerFactory;


public class SkippedRecipeConditionAction extends MappingDispatchAction{
	
	private static final Logger LOGGER = Logger.getLogger(SkippedRecipeConditionAction.class);
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException 
	 */
	public ActionForward saveSkippedCondition(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws SQLException 
			 {
		
		LOGGER.debug("Begin SkippedRecipeConditionAction.saveSkippedCondition");
		
		//extract object from request
		SkippedFshRcpCon skippedFshRcpCon = ConverterUtility.extractSkpFshRcpCon(request);
		
		// create skipped finished recipe manager instance
		SkpFshRcpManager skpFshRcpManager = ManagerFactory.getInstance().getSkpFshRcpConManager();
		
		// insert to database
		skpFshRcpManager.insertSkippedFshRcpCon(skippedFshRcpCon);
		
		LOGGER.debug("End SkippedRecipeConditionAction.saveSkippedCondition");
		
        return mapping.findForward("success");
	}
	
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException 
	 */
	public ActionForward loadSkippedCondition(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws SQLException 
			{
		LOGGER.debug("Begin SkippedRecipeConditionAction.loadSkippedCondition");
		
		// create skipped finished recipe manager instance
		SkpFshRcpManager skpFshRcpManager = ManagerFactory.getInstance().getSkpFshRcpConManager();
		
		// load skipped finished recipe conditions
		SkippedFshRcpCon skippedFshRcpCon = skpFshRcpManager.getSkpFshRcpCon();
		
		// push loaded values to form
		((SkippedRecipeConditionForm)form).setBaseLeadTime(skippedFshRcpCon.getBaseLeadTimeVal());
		((SkippedRecipeConditionForm)form).setSelectedBaseLeadTimeOperator(skippedFshRcpCon.getBaseLeadTimeOp());
		((SkippedRecipeConditionForm)form).setRunTime(skippedFshRcpCon.getRunTimeVal());
		((SkippedRecipeConditionForm)form).setSelectedRunTimeOperators(skippedFshRcpCon.getRunTimeOp());
		((SkippedRecipeConditionForm)form).setWaitTime(skippedFshRcpCon.getWaitTimeVal());
		((SkippedRecipeConditionForm)form).setSelectedWaitTimeOperators(skippedFshRcpCon.getWaitTimeOp());
		((SkippedRecipeConditionForm)form).setLastModified(skippedFshRcpCon.getLastModified());
		
		LOGGER.debug("End SkippedRecipeConditionAction.loadSkippedCondition");
        return mapping.findForward("success");
	}
}
